#include <stdlib.h>
#include <stdio.h>

#include "file_status.h"

char *FS_NAMES[]={
    "Unknown",
    "Synchronised",
    "Absent",
    "Newer",
    "Older",
    "Old"
};

int FS_set(sFS *fsl, unsigned int fsllen) {
    if(!fsllen || !fsl) return 1;

    unsigned int i;
    time_t older, newer;
    for(i=0; i<fsllen; i++) {
        time_t mtime=fsl[i].stat.st_mtime;

        if(!i || mtime<older)
            older=mtime;
        if(!i || mtime>newer)
            newer=mtime;
    }

    if(older==newer) {
        if(!older) {
            printf("FS_set wtf ?\n");
            return 1;
        }

        for(i=0; i<fsllen; i++)
            fsl[i].status=FS_SYNCED;
        return 0;
    }

    for(i=0; i<fsllen; i++) {
        time_t mtime=fsl[i].stat.st_mtime;
        if(mtime==older){
            if(mtime)
                fsl[i].status=FS_OLDER;
            else
                fsl[i].status=FS_ABSENT;    // TODO check better
        }
        else if(mtime==newer)
            fsl[i].status=FS_NEWER;
        else
            fsl[i].status=FS_OLD;
    }

    return 0;
}

